package com.gnerv.author.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gnerv.author.dto.ArticalCountDTO;
import com.gnerv.author.entity.Commentary;
import com.gnerv.author.entity.R;
import com.gnerv.author.service.ICommentaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 博客平台-评论表 前端控制器
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-07-19
 */
@RestController
@RequestMapping("/blog/commentary")
public class CommentaryController {
    @Autowired
    ICommentaryService commentService;

    @PostMapping("/create")
    public R createComment(HttpServletRequest request, @RequestBody Commentary comment) {
        if (request.getHeader("x-forwarded-for") == null) {
            comment.setIp(request.getRemoteAddr());
        } else {
            comment.setIp(request.getHeader("x-forwarded-for"));
        }
        commentService.createOrUpdate(comment);
        return R.back();
    }

    @RequiresPermissions("blog:article:delete")
    @GetMapping("/delete/{bId}")
    public R deleteComment(@PathVariable("bId") String bId) {
        commentService.deleteCommentary(bId);
        return R.back();
    }

    @RequiresPermissions("blog:commentary:update")
    @PostMapping("/update")
    public R updateComment(@RequestBody Commentary comment) {
        commentService.createOrUpdate(comment);
        return R.back();
    }

    @GetMapping("select/{bId}")
    public R selectOne(@PathVariable("bId") String bId) {
        Commentary comment = commentService.selectCommentary(bId);
        return R.back().data("comment", comment);
    }

    @PostMapping("/list/{aId}")
    public R listUnverify(@PathVariable("aId") String aId) {
        List<Commentary> list = commentService.listUnverify(aId);
        return R.back().data("comments", list);
    }

    @PostMapping("/list_verify/{aId}")
    public R listVerify(@PathVariable("aId") String aId) {
        List<Commentary> list = commentService.listCommentary(new Commentary(aId, 1));
        return R.back().data("comments", list);
    }


    @PostMapping("/sel_artical_manage")
    public R articalManage() {
        List<ArticalCountDTO> articalCountDTOS = commentService.countArticalByUnverify();
        return R.back().data("articalCountDTOS", articalCountDTOS);
    }

}
